<template>
  <page-wrap stay-still back hide-tab title="简历详情">
    <view class="resume-detail">
      <view class="resume-top">
        <view class="box top-info">
          <image :src="logo" mode="aspectFill" class="logo" />
          <view class="num-time">
            <view class="text num"
              >简历编号：{{ resumeData.personNumber_p }}</view
            >
            <view class="text time"
              >更新时间：{{ resumeData.updatetime_p }}</view
            >
          </view>
        </view>
        <PInfo :resumeData="resumeData" :personId="id" :status="status" />

        <view v-if="showContact" class="box contact">
          <view class="box-title">联系方式</view>
          <view class="box-item"> 手机：{{ resumeData.Shouji_p }} </view>
          <view> 邮箱：{{ resumeData.Email_p }} </view>
        </view>
        <AimList v-if="allAimList.length" :allAimList="allAimList" />
        <Exp
          v-if="resumeData.personWorkArr && resumeData.personWorkArr.length"
          :personWorkArr="resumeData.personWorkArr"
        />
        <Project
          v-if="resumeData.work_projectArr && resumeData.work_projectArr.length"
          :work_projectArr="resumeData.work_projectArr"
        />
        <Edu
          v-if="resumeData.personEdusArr && resumeData.personEdusArr.length"
          :personEdusArr="resumeData.personEdusArr"
        />
        <Train
          v-if="resumeData.trainArr && resumeData.trainArr.length"
          :trainArr="resumeData.trainArr"
        />
        <Cer
          v-if="resumeData.personCerArr && resumeData.personCerArr.length"
          :personCerArr="resumeData.personCerArr"
        />
        <Train
          v-if="personInfo.trainArr"
          :trainArr="personInfo.trainArr"
          titleName="实习经历"
        />
        <view v-if="showContact" class="box annex">
          <view class="box-title">附件简历</view>
          <view class="annex-text">
            <text
              v-if="resumeData.fujian && resumeData.fujian.filepath"
              @click="toCheckAnnex(resumeData.fujian)"
              >已上传附件简历，您可以
              <text class="annex-check">【点击查看】</text>
            </text>
            <text v-else>暂未上传附件简历</text>
          </view>
        </view>
      </view>
      <view v-if="showBottom && loginStatus == 'company'" class="resume-bottom">
        <view
          class="btn btn-contact"
          :class="{ disable: contactDisable }"
          @click="getContactInfo"
        >
          {{ contactText }}
        </view>
        <view class="btn btn-connect" @click="handleConnect"> 立即沟通 </view>
      </view>
    </view>
  </page-wrap>
</template>

<script>
import { getResumeData, getMyIntendedTradeList } from "@/api";
import {
  checkHadDownOrDelivery,
  checkCompanyPersonRight,
  isCheckedResume,
  jianliDownloadNew,
  doCompanyPersonRightsConsume,
  updateCmailboxNew,
} from "@/api/modules/company";
import IMEventHandler from "@/utils/im";

import { eduList } from "@/utils/data";

import Exp from "../components/Exp.vue";
import Project from "../components/Project.vue";
import Edu from "../components/Edu.vue";
import Train from "../components/Train";
import Cer from "../components/Cer.vue";
import Skill from "../components/Skill.vue";
import PInfo from "../components/PInfo.vue";
import AimList from "../components/AimList.vue";

import ShareMixin from "@/mixins/share";
import LoginMixin from "@/mixins/login.js";

export default {
  mixins: [ShareMixin, LoginMixin],
  components: {
    Exp,
    Project,
    Edu,
    Train,
    Cer,
    Skill,
    PInfo,
    AimList,
  },
  data() {
    return {
      id: "",
      companyId: uni.getStorageSync("company-id") || "",
      personId: uni.getStorageSync("person-id").slice(2) || undefined,
      loginStatus: uni.getStorageSync("login-status") || "person",
      resumeData: {},
      resumeDataCopy: {},
      manImg: this.$imgBase + "resume/man.png",
      womanImg: this.$imgBase + "resume/woman.png",
      allAimList: [],
      allAimListCopy: [],
      status: "",
      showContact: false,
      logo:
        this.$imgBaseSelf +
        "company/resume_logo.png?v=" +
        getApp().globalData.nowDate,
      showResumeViewCard: false,
      showChatCard: false,
      showConnectCard: false,
      contactDisable: false,
      contactText: "获取联系方式",
      rights: {
        checkResume: false,
        chat: false,
        commonContact: false,
        highContact: false,
        personContact: false,
      },
      connectType: "general",
      needChatRights: true, // 是否需要消耗联系卡
      showBottom: false,
      hadDelivery: false,
      shareTitle: "简历详情 - " + this.$selfName,
    };
  },
  filters: {
    eduFilter(val) {
      let item = eduList.find((ele) => {
        return ele.value == val;
      });
      return item.label;
    },
  },
  onLoad(opt) {
    this.id = opt && opt.personId;
    this.sharePath = "/pagesSub/resume/resumeDetail/index?personId=" + this.id;
    this.shareQuery = "personId=" + this.id;
    if (this.loginStatus == "person") {
      uni.showModal({
        title: "提示",
        content: "简历仅企业能查看",
        showCancel: false,
        success: function (res) {
          uni.redirectTo({
            url: "/pages/index/index",
          });
        },
      });
    }
  },
  mounted() {
    this.getData();
    this.getMyIntendedTradeList();
    this.checkCompanyPersonRight("CHAT");
    // this.checkCompanyPersonRight('COMMON_CONTACT');
    // this.checkCompanyPersonRight('HIGH_CONTACT');
    this.checkCompanyPersonRight("person_contact");
    this.imLogin();
  },
  methods: {
    getData() {
      let params = {
        company_id: this.companyId,
        resume_person_id: this.id,
        person_id: this.personId,
      };
      checkHadDownOrDelivery(params).then((res) => {
        this.showContact = res.data && res.data.is_allow == 1;
        if (res.data && res.data.had_delivery == 1) {
          this.contactText = "对方已投递";
          this.hadDelivery = true;
        } else if (res.data && res.data.had_down == 1) {
          this.contactText = "已获取";
        }
        if (this.showContact) {
          this.getResumeData();
          this.contactDisable = true;
          // this.resumeData = this.resumeDataCopy;
          this.allAimList = this.allAimListCopy;
        } else {
          this.getResumeData();
          this.allAimList = this.allAimListCopy;

          // this.isCheckedResume().then(checkRes => {
          //   if(checkRes.code == 200) {
          //     // this.resumeData = this.resumeDataCopy;
          //     this.getResumeData();
          //     this.allAimList = this.allAimListCopy;
          //   } else {
          //     this.checkCompanyPersonRight('CHECK_RESUME');
          //   }
          // })
        }
      });
    },
    getResumeData() {
      let param = {
        personId: this.id,
        conditionArr: JSON.stringify({
          is_mp: 1,
        }),
      };
      getResumeData(param).then((res) => {
        if (res.code == 200) {
          this.resumeData = res.data;
          if (this.resumeData.Grzz_p) {
            this.resumeData.Grzz_p = this.resumeData.Grzz_p.replace(
              /<br \/>/g,
              "\n"
            ).replace(/&nbsp;/g, " ");
          }
          this.showBottom = true;
          this.updateCmailboxNew();
        } else {
          uni.showToast({
            title: res.status_desc,
            icon: "none",
            duration: 1000
          });
        }
      });
    },
    updateCmailboxNew() {
      const params = {
        company_id: this.companyId,
        person_id: this.id,
        cmail_id: "1",
      };
      updateCmailboxNew(params);
    },
    getMyIntendedTradeList() {
      let params = {
        personId: this.id,
      };
      getMyIntendedTradeList(params).then((res) => {
        if (res.code == 200) {
          this.allAimListCopy = res.data.myTended || [];
          this.status = +res.data.personInfo.person_status;
        }
      });
    },

    imLogin() {
      // 是否登录im
      var isIMLogin =
        getApp().globalData.tim && getApp().globalData.tim.getLoginUser();
      if (!isIMLogin) {
        new IMEventHandler(() => {
          this.getMsgList();
        });
      } else {
        this.getMsgList();
      }
    },

    handleConnect() {
      if (!this.personId) {
        return uni.showToast({
          title: "请先登录",
          icon: "none",
          complete() {
            let fullPath =
              getCurrentPages()[getCurrentPages().length - 1].$page.fullPath;
            let returnUrl = encodeURIComponent(fullPath);
            setTimeout(() => {
              this.handleLoginPage();
            }, 1500);
          },
        });
      }
      if (this.hadDelivery) {
        this.toChat();
        return;
      }
      if (this.needChatRights && !this.rights.chat) {
        // this.showChatCard = true;
        this.showRightsTips();
        return;
      }
      this.toChat();
      // if(this.needChatRights) {
      //   uni.showLoading();
      //   this.rightsConsume('CHAT').then(res => {
      //     uni.hideLoading();
      //     if(res.code == 200) {
      //       this.toChat();
      //     } else {
      //       uni.showToast({
      //         title: res.status_desc || '沟通卡使用失败',
      //         icon: 'none',
      //       })
      //     }
      //   })
      // } else {
      // this.toChat();
      // }
    },

    toChat() {
      // 是否登录im
      var isIMLogin =
        getApp().globalData.tim && getApp().globalData.tim.getLoginUser();
      if (!isIMLogin) {
        uni.showLoading({
          title: "正在连接",
          mask: true,
        });
        let isFirst = true;
        new IMEventHandler(() => {
          if (isFirst) {
            isFirst = false;
            uni.hideLoading();
            uni.navigateTo({
              url: "/pagesSub/message/chat/index?chatId=" + this.id,
            });
          }
        });
      } else {
        uni.navigateTo({
          url: "/pagesSub/message/chat/index?chatId=" + this.id,
        });
      }
    },
    handleResumeViewClose() {
      this.showResumeViewCard = false;
    },
    handlBack() {
      uni.navigateBack({
        fail: () => {
          uni.switchTab({
            url: "/pages/index/index",
          });
        },
      });
    },
    handleChatCardClose() {
      this.showChatCard = false;
    },
    handleConnectCardClose() {
      this.showConnectCard = false;
    },

    // 获取联系方式
    getContactInfo() {
      console.log("简历下载----------");
      if (this.contactDisable) return;

      // let isGj = this.resumeData.gj_p == 2;

      // if(isGj) {
      //   this.connectType = 'vip';
      //   if(!this.rights.highContact) {
      //     this.showConnectCard = true;
      //     return;
      //   }
      // } else {
      //   this.connectType = 'general';
      //   if(!this.rights.commonContact) {
      //     this.showConnectCard = true;
      //     return;
      //   }
      // }

      if (!this.rights.personContact) {
        //   console.log("弹出卡牌----------");
        //   this.showConnectCard = true;
        this.showRightsTips();
        return;
      }

      uni.showLoading();

      let params = {
        person_id: this.personId,
        resume_person_id: this.id,
        conditionArr: JSON.stringify({
          source: "mp",
        }),
      };
      console.log("简历下载----------", params);
      jianliDownloadNew(params).then((res) => {
        uni.hideLoading();
        if (res.code == 200 || res.code == 10) {
          this.showContact = true;
          this.contactDisable = true;
          this.contactText = "已获取";

          // let type = isGj? 'HIGH_CONTACT': 'COMMON_CONTACT';
          // if(res.code == 200) this.rightsConsume('person_contact');
        } else {
          uni.showToast({
            title: res.status_desc || "获取失败",
            icon: "none",
            duration: 1000
          });
        }
      });
    },

    isCheckedResume() {
      const params = {
        company_id: this.companyId,
        resume_person_id: this.id,
      };
      return isCheckedResume(params);
    },

    // 权益校验接口
    checkCompanyPersonRight(type) {
      const params = {
        personId: this.personId,
        companyId: this.companyId,
        checkType: type,
      };
      checkCompanyPersonRight(params).then((res) => {
        if (type == "CHECK_RESUME") {
          if (res.code == 200) {
            this.resumeRightsConsume();
          } else {
            this.showResumeViewCard = true;
          }
        } else {
          if (res.code == 200) {
            if (type == "CHAT") {
              this.rights.chat = true;
            }
            // else if(type == 'COMMON_CONTACT') {
            //   this.rights.commonContact = true;
            // } else if(type == 'HIGH_CONTACT') {
            //   this.rights.highContact = true;
            // }
            else if (type == "person_contact") {
              this.rights.personContact = true;
            }
          }
        }
      });
    },
    handleResumeViewSuccess() {
      // todo 购买成功后
      this.rights.checkResume = true;
      this.showResumeViewCard = false;
      this.resumeRightsConsume();
    },
    handleChatSuccess() {
      this.rights.chat = true;
      this.handleConnect();
    },
    handleConnectSuccess() {
      // this.rights.commonContact = true;
      // this.rights.highContact = true;
      this.rights.personContact = true;
      this.getContactInfo();
    },

    // 聊天记录判断是否需要消耗沟通卡
    getMsgList() {
      // let param = {
      //   condition_arr: JSON.stringify({
      //     send_uid: getApp().globalData.imInfo.username,
      //     receive_uid: this.id,
      //     listtype: "pageList",
      //     pageindex: 1,
      //     person_status: 1,
      //     search_until: '0000-00-00 00:00:00',
      //   }),
      // };
      // getMsgList(param).then((res) => {
      //   if(res && res.data && res.data.length) {
      //     this.needChatRights = false;
      //   } else {
      //     this.needChatRights = true;
      //   }
      // })
      let chatList = getApp().globalData.messageList.filter(
        (ele) => ele.from_username == this.id
      );
      if (chatList.length) {
        this.needChatRights = false;
      } else {
        this.needChatRights = true;
      }
    },
    rightsConsume(checkType) {
      const params = {
        personId: this.personId,
        companyId: this.companyId,
        checkType: checkType,
      };
      return doCompanyPersonRightsConsume(params);
    },

    // 简历查看消耗
    resumeRightsConsume() {
      this.rightsConsume("CHECK_RESUME").then((res) => {
        if (res.code == 200) {
          this.rights.checkResume = true;
          this.getResumeData();
          this.allAimList = this.allAimListCopy;
        } else {
          uni.showToast({
            title: res.status_desc || "简历查看权益消耗错误",
            icon: "none",
            duration: 1000
          });
        }
      });
    },

    toCheckAnnex(item) {
      let name = item.filename || this.resumeData.Iname_p + "的简历" || "";
      uni.navigateTo({
        url:
          "/pagesSub/public/file/index?title=简历附件&name=" +
          name +
          "&path=" +
          encodeURIComponent(item.filepath),
      });
    },
    // 权益不足提示
    showRightsTips() {
      uni.showModal({
        title: "提示",
        content:
          "您没有相关剩余权益，若需要继续使用当前功能，请您联系专属顾问400-884-1001",
        confirmText: "取消",
        confirmColor: "#999999",
        cancelText: "立即联系",
        cancelColor: "#F75A3E",
        success: (modalRes) => {
          if (!modalRes.confirm) {
            uni.makePhoneCall({
              phoneNumber: "400-884-1001",
            });
          }
        },
      });
    },
  },
};
</script>

<style lang="scss">
$img-base-resume: $img-base + "resume/";
.resume-detail {
  padding-bottom: 120rpx;
  .resume-top {
    padding: 32rpx;
    .box {
      margin-bottom: $uni-spacing-col-big;
      padding: $uni-spacing-col-big;
      background-color: $uni-bg-color;
      border-radius: $uni-border-radius-lg;
      &-title {
        margin-bottom: $uni-spacing-col-lg;
        font-size: $uni-font-size-lg;
        font-weight: bold;
        color: #333333;
      }
      .icon {
        width: 36rpx;
        height: 36rpx;
        vertical-align: bottom;
      }
    }
    .box-item {
      margin-bottom: $uni-spacing-col-big;
      &:last-child {
        margin-bottom: 0;
      }
    }
    .top-info {
      display: flex;
      justify-content: space-between;
      margin-bottom: 0;
      background-color: transparent;
      .num-time {
        font-size: $uni-font-size-sm;
        color: $uni-text-color-999;
        .text {
          display: block;
          line-height: 40rpx;
        }
      }
    }

    .contact {
      font-size: $uni-font-size-sm;
      .box-item {
        margin-bottom: 10rpx;
      }
    }
    .item-title {
      margin-bottom: $uni-spacing-col-lg;
      font-size: $uni-font-size-base;
      font-weight: bold;
      color: $uni-text-color-333;
    }

    .annex {
      &-text {
        font-size: 24rpx;
      }
      &-check {
        color: $u-type-primary;
      }
    }
  }
  .resume-bottom {
    position: fixed;
    bottom: 0;
    width: 100%;
    padding: 32rpx;
    padding-bottom: calc(32rpx + constant(safe-area-inset-bottom));
    padding-bottom: calc(32rpx + env(safe-area-inset-bottom));
    background-color: $uni-bg-color;

    .btn-contact {
      width: 230rpx;
      margin-right: 30rpx;
      color: $u-type-primary;
      border: 2rpx solid $u-type-primary;
      &.disable {
        color: #999999;
        border-color: #999999;
      }
    }
    .btn-connect {
      width: 426rpx;
      // width: 100%;
      color: #fff;
      background: $bg-linear-gradient-button;
    }
  }
  .logo {
    width: 110rpx;
    height: 80rpx;
  }
  .divide {
    margin: 0 12rpx;
    color: $uni-text-color-999;
  }
}
</style>
